package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.service.CartInfoService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.cart.CartInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 购物车的controller
 * @author 搭忆长安
 * @version 1.0
 * @date 2021年07月09日 19:52
 * @ClassName:CartController
 * @Description: TODO
 */
@RestController
@RequestMapping(value = "/cart")
public class CartController {
    @Autowired
    private CartInfoService cartInfoService;

    @Autowired
    private HttpServletRequest request;

    @GetMapping(value = "/add")
    public Result addCart(Long skuId,Integer num){
        //获取用户的id
        String userId = "1";
        String tempUserId = request.getHeader("tempUserId");
        //String tempUserId="";
        if(!StringUtils.isEmpty(userId)){
            //登录用户的新增
            cartInfoService.addCart(skuId,num,userId);
            return Result.ok();
        }else{
            //临时用户的新增
            String s = cartInfoService.addCartTemp(skuId, num, tempUserId);
            return Result.ok(s);
        }
    }

    /**
     * 查询购物车数据
     * @return
     */
    @GetMapping(value = "/getCart")
    public Result getCart(){
        //测试一,都为空--返回空(实践一样)
//        String userId = "";
//        String tempUserId = "";
        //测试二,返回登录用户的购物车数据即可(实践一样)
//        String userId = "2";
//        String tempUserId = "";
        //测试三, 返回临时用户的购物车信息即可(实践一样)
//        String userId = "";
//        String tempUserId = "2";
        //返回数据成功
        String userId = "2";
        String tempUserId = "1";
        List<CartInfo> cartInfoList = cartInfoService.getCartInfoList(userId, tempUserId);
        return  Result.ok(cartInfoList);
    }
    /**
     * 查询用户选中的购物车信息
     * @return
     */
    @GetMapping(value = "/getChooseCartList")
    public Result getChooseCartList(){
        Long userId = 2L;
        Map<String, Object> chooseCartInfo = cartInfoService.getChooseCartInfo(userId);
        return Result.ok(chooseCartInfo);
    }

    /**
     * 查询用户选中的购物车信息
     * @return
     */
    @GetMapping(value = "/getChooseCartListByUser/{userId}")
    public Result<Map<String, Object>> getChooseCartList(@PathVariable(value = "userId") Long userId){
        Map<String, Object> chooseCartInfo = cartInfoService.getChooseCartInfo(userId);
        return Result.ok(chooseCartInfo);
    }

    /**
     * 查询用户选中的购物车信息
     * @return
     */
    @GetMapping(value = "/getChooseCartInfoList/{userId}")
    public List<CartInfo> getChooseCartInfoList(@PathVariable(value = "userId") Long userId){
        return cartInfoService.getChooseCartInfoList(userId);
    }
    
    /**
     * 删除购物车
     * @param userId
     * @param skuId
     * @return
     */
    @GetMapping(value = "/delCart/{userId}/{skuId}")
    public Result delCart(@PathVariable(value = "userId")Long userId,@PathVariable(value = "skuId")Long skuId){
        cartInfoService.deleteCart(userId, skuId);
        return Result.ok();
    }


}
